home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / src / interp / perl5.005.tar.gz / perl5.005.tar / perl5.005 / x2p / find2perl.PL < prev    next >
Perl Script  |  1998-06-12  |  14KB  |  628 lines

  1. #!/usr/local/bin/perl
  2.  
  3. use Config;
  4. use File::Basename qw(&basename &dirname);
  5. use Cwd;
  6.  
  7. # List explicitly here the variables you want Configure to
  8. # generate.  Metaconfig only looks for shell variables, so you
  9. # have to mention them as if they were shell variables, not
  10. # %Config entries.  Thus you write
  11. #  $startperl
  12. # to ensure Configure will look for $Config{startperl}.
  13.  
  14. # This forces PL files to create target in same directory as PL file.
  15. # This is so that make depend always knows where to find PL derivatives.
  16. $origdir = cwd;
  17. chdir dirname($0);
  18. $file = basename($0, '.PL');
  19. $file .= '.com' if $^O eq 'VMS';
  20.  
  21. open OUT,">$file" or die "Can't create $file: $!";
  22.  
  23. print "Extracting $file (with variable substitutions)\n";
  24.  
  25. # In this section, perl variables will be expanded during extraction.
  26. # You can use $Config{...} to use Configure variables.
  27.  
  28. print OUT <<"!GROK!THIS!";
  29. $Config{startperl}
  30.     eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
  31.       if \$running_under_some_shell;
  32. \$startperl = "$Config{startperl}";
  33. \$perlpath = "$Config{perlpath}";
  34. !GROK!THIS!
  35.  
  36. # In the following, perl variables are not expanded during extraction.
  37.  
  38. print OUT <<'!NO!SUBS!';
  39.  
  40. # Modified September 26, 1993 to provide proper handling of years after 1999
  41. #   Tom Link <tml+@pitt.edu>
  42. #   University of Pittsburgh
  43. # Modified April 7, 1998 with nasty hacks to implement the troublesome -follow
  44. #  Billy Constantine <wdconsta@cs.adelaide.edu.au> <billy@smug.adelaide.edu.au>
  45. #  University of Adelaide, Adelaide, South Australia
  46.  
  47. while ($ARGV[0] =~ /^[^-!(]/) {
  48.     push(@roots, shift);
  49. }
  50. @roots = ('.') unless @roots;
  51. for (@roots) { $_ = "e($_); }
  52. $roots = join(',', @roots);
  53.  
  54. $indent = 1;
  55. $stat = 'lstat';
  56. $decl = '';
  57.  
  58. while (@ARGV) {
  59.     $_ = shift;
  60.     s/^-// || /^[()!]/ || die "Unrecognized switch: $_\n";
  61.     if ($_ eq '(') {
  62.     $out .= &tab . "(\n";
  63.     $indent++;
  64.     next;
  65.     }
  66.     elsif ($_ eq ')') {
  67.     $indent--;
  68.     $out .= &tab . ")";
  69.     }
  70.     elsif ($_ eq 'follow') {
  71.     $stat = 'stat';
  72.     $decl = '%already_seen = ();';
  73.     $out .= &tab . '(not $already_seen{"$dev,$ino"}) &&';
  74.     $out .= "\n" . &tab . '(($already_seen{"$dev,$ino"} = !(-d _)) || 1)';
  75.     }
  76.     elsif ($_ eq '!') {
  77.     $out .= &tab . "!";
  78.     next;
  79.     }
  80.     elsif ($_ eq 'name') {
  81.     $out .= &tab;
  82.     $pat = &fileglob_to_re(shift);
  83.     $out .= '/' . $pat . "/";
  84.     }
  85.     elsif ($_ eq 'perm') {
  86.     $onum = shift;
  87.     die "Malformed -perm argument: $onum\n" unless $onum =~ /^-?[0-7]+$/;
  88.     if ($onum =~ s/^-//) {
  89.         $onum = '0' . sprintf("%o", oct($onum) & 017777);    # s/b 07777 ?
  90.         $out .= &tab . "((\$mode & $onum) == $onum)";
  91.     }
  92.     else {
  93.         $onum = '0' . $onum unless $onum =~ /^0/;
  94.         $out .= &tab . "((\$mode & 0777) == $onum)";
  95.     }
  96.     }
  97.     elsif ($_ eq 'type') {
  98.     ($filetest = shift) =~ tr/s/S/;
  99.     $out .= &tab . "-$filetest _";
  100.     }
  101.     elsif ($_ eq 'print') {
  102.     $out .= &tab . 'print("$name\n")';
  103.     }
  104.     elsif ($_ eq 'print0') {
  105.     $out .= &tab . 'print("$name\0")';
  106.     }
  107.     elsif ($_ eq 'fstype') {
  108.     $out .= &tab;
  109.     $type = shift;
  110.     if ($type eq 'nfs')
  111.         { $out .= '($dev < 0)'; }
  112.     else
  113.         { $out .= '($dev >= 0)'; }
  114.     }
  115.     elsif ($_ eq 'user') {
  116.     $uname = shift;
  117.     $out .= &tab . "(\$uid == \$uid{'$uname'})";
  118.     $inituser++;
  119.     }
  120.     elsif ($_ eq 'group') {
  121.     $gname = shift;
  122.     $out .= &tab . "(\$gid == \$gid{'$gname'})";
  123.     $initgroup++;
  124.     }
  125.     elsif ($_ eq 'nouser') {
  126.     $out .= &tab . '!defined $uid{$uid}';
  127.     $inituser++;
  128.     }
  129.     elsif ($_ eq 'nogroup') {
  130.     $out .= &tab . '!defined $gid{$gid}';
  131.     $initgroup++;
  132.     }
  133.     elsif ($_ eq 'links') {
  134.     $out .= &tab . '($nlink ' . &n(shift);
  135.     }
  136.     elsif ($_ eq 'inum') {
  137.     $out .= &tab . '($ino ' . &n(shift);
  138.     }
  139.     elsif ($_ eq 'size') {
  140.     $out .= &tab . '(int(((-s _) + 511) / 512) ' . &n(shift);
  141.     }
  142.     elsif ($_ eq 'atime') {
  143.     $out .= &tab . '(int(-A _) ' . &n(shift);
  144.     }
  145.     elsif ($_ eq 'mtime') {
  146.     $out .= &tab . '(int(-M _) ' . &n(shift);
  147.     }
  148.     elsif ($_ eq 'ctime') {
  149.     $out .= &tab . '(int(-C _) ' . &n(shift);
  150.     }
  151.     elsif ($_ eq 'exec') {
  152.     for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
  153.     shift;
  154.     $_ = "@cmd";
  155.     if (m#^(/bin/)?rm -f {}$#) {
  156.         if (!@ARGV) {
  157.         $out .= &tab . 'unlink($_)';
  158.         }
  159.         else {
  160.         $out .= &tab . '(unlink($_) || 1)';
  161.         }
  162.     }
  163.     elsif (m#^(/bin/)?rm {}$#) {
  164.         $out .= &tab . '(unlink($_) || warn "$name: $!\n")';
  165.     }
  166.     else {
  167.         for (@cmd) { s/'/\\'/g; }
  168.         $" = "','";
  169.         $out .= &tab . "&exec(0, '@cmd')";
  170.         $" = ' ';
  171.         $initexec++;
  172.     }
  173.     }
  174.     elsif ($_ eq 'ok') {
  175.     for (@cmd = (); @ARGV && $ARGV[0] ne ';'; push(@cmd,shift)) { }
  176.     shift;
  177.     for (@cmd) { s/'/\\'/g; }
  178.     $" = "','";
  179.     $out .= &tab . "&exec(1, '@cmd')";
  180.     $" = ' ';
  181.     $initexec++;
  182.     }
  183.     elsif ($_ eq 'prune') {
  184.     $out .= &tab . '($prune = 1)';
  185.     }
  186.     elsif ($_ eq 'xdev') {
  187.     $out .= &tab . '!($prune |= ($dev != $topdev))';
  188.     }
  189.     elsif ($_ eq 'newer') {
  190.     $out .= &tab;
  191.     $file = shift;
  192.     $newername = 'AGE_OF' . $file;
  193.     $newername =~ s/[^\w]/_/g;
  194.     $newername = "\$$newername";
  195.     $out .= "(-M _ < $newername)";
  196.     $initnewer .= "$newername = -M " . "e($file) . ";\n";
  197.     }
  198.     elsif ($_ eq 'eval') {
  199.     $prog = "e(shift);
  200.     $out .= &tab . "eval $prog";
  201.     }
  202.     elsif ($_ eq 'depth') {
  203.     $depth++;
  204.     next;
  205.     }
  206.     elsif ($_ eq 'ls') {
  207.     $out .= &tab . "&ls";
  208.     $initls++;
  209.     }
  210.     elsif ($_ eq 'tar') {
  211.     $out .= &tab;
  212.     die "-tar must have a filename argument\n" unless @ARGV;
  213.     $file = shift;
  214.     $fh = 'FH' . $file;
  215.     $fh =~ s/[^\w]/_/g;
  216.     $out .= "&tar($fh)";
  217.     $file = '>' . $file;
  218.     $initfile .= "open($fh, " . "e($file) .
  219.       qq{) || die "Can't open $fh: \$!\\n";\n};
  220.     $inittar++;
  221.     $flushall = "\n&tflushall;\n";
  222.     }
  223.     elsif (/^n?cpio$/) {
  224.     $depth++;
  225.     $out .= &tab;
  226.     die "-$_ must have a filename argument\n" unless @ARGV;
  227.     $file = shift;
  228.     $fh = 'FH' . $file;
  229.     $fh =~ s/[^\w]/_/g;
  230.     $out .= "&cpio('" . substr($_,0,1) . "', $fh)";
  231.     $file = '>' . $file;
  232.     $initfile .= "open($fh, " . "e($file) .
  233.       qq{) || die "Can't open $fh: \$!\\n";\n};
  234.     $initcpio++;
  235.     $flushall = "\n&flushall;\n";
  236.     }
  237.     else {
  238.     die "Unrecognized switch: -$_\n";
  239.     }
  240.     if (@ARGV) {
  241.     if ($ARGV[0] eq '-o') {
  242.         { local($statdone) = 1; $out .= "\n" . &tab . "||\n"; }
  243.         $statdone = 0 if $indent == 1 && $delayedstat;
  244.         $saw_or++;
  245.         shift;
  246.     }
  247.     else {
  248.         $out .= " &&" unless $ARGV[0] eq ')';
  249.         $out .= "\n";
  250.         shift if $ARGV[0] eq '-a';
  251.     }
  252.     }
  253. }
  254.  
  255. print <<"END";
  256. $startperl
  257.     eval 'exec $perlpath -S \$0 \${1+"\$@"}'
  258.     if \$running_under_some_shell;
  259.  
  260. END
  261.  
  262. if ($initls) {
  263.     print <<'END';
  264. @rwx = ('---','--x','-w-','-wx','r--','r-x','rw-','rwx');
  265. @moname = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec);
  266.  
  267. END
  268. }
  269.  
  270. if ($inituser || $initls) {
  271.     print 'while (($name, $pw, $uid) = getpwent) {', "\n";
  272.     print '    $uid{$name} = $uid{$uid} = $uid;', "\n" if $inituser;
  273.     print '    $user{$uid} = $name unless $user{$uid};', "\n" if $initls;
  274.     print "}\n\n";
  275. }
  276.  
  277. if ($initgroup || $initls) {
  278.     print 'while (($name, $pw, $gid) = getgrent) {', "\n";
  279.     print '    $gid{$name} = $gid{$gid} = $gid;', "\n" if $initgroup;
  280.     print '    $group{$gid} = $name unless $group{$gid};', "\n" if $initls;
  281.     print "}\n\n";
  282. }
  283.  
  284. print $initnewer, "\n" if $initnewer;
  285.  
  286. print $initfile, "\n" if $initfile;
  287.  
  288. $find = $depth ? "finddepth" : "find";
  289. print <<"END";
  290. require "$find.pl";
  291.  
  292. # Traverse desired filesystems
  293.  
  294. $decl
  295. &$find($roots);
  296. $flushall
  297. exit;
  298. sub wanted {
  299. $out;
  300. }
  301.  
  302. END
  303.  
  304. if ($initexec) {
  305.     print <<'END';
  306. sub exec {
  307.     local($ok, @cmd) = @_;
  308.     foreach $word (@cmd) {
  309.     $word =~ s#{}#$name#g;
  310.     }
  311.     if ($ok) {
  312.     local($old) = select(STDOUT);
  313.     $| = 1;
  314.     print "@cmd";
  315.     select($old);
  316.     return 0 unless <STDIN> =~ /^y/;
  317.     }
  318.     chdir $cwd;        # sigh
  319.     system @cmd;
  320.     chdir $dir;
  321.     return !$?;
  322. }
  323.  
  324. END
  325. }
  326.  
  327. if ($initls) {
  328.     print <<"INTERP", <<'END';
  329. sub ls {
  330.     (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$sizemm,
  331.       \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
  332. INTERP
  333.  
  334.     $pname = $name;
  335.  
  336.     if (defined $blocks) {
  337.     $blocks = int(($blocks + 1) / 2);
  338.     }
  339.     else {
  340.     $blocks = int(($size + 1023) / 1024);
  341.     }
  342.  
  343.     if    (-f _) { $perms = '-'; }
  344.     elsif (-d _) { $perms = 'd'; }
  345.     elsif (-c _) { $perms = 'c'; $sizemm = &sizemm; }
  346.     elsif (-b _) { $perms = 'b'; $sizemm = &sizemm; }
  347.     elsif (-p _) { $perms = 'p'; }
  348.     elsif (-S _) { $perms = 's'; }
  349.     else         { $perms = 'l'; $pname .= ' -> ' . readlink($_); }
  350.  
  351.     $tmpmode = $mode;
  352.     $tmp = $rwx[$tmpmode & 7];
  353.     $tmpmode >>= 3;
  354.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  355.     $tmpmode >>= 3;
  356.     $tmp = $rwx[$tmpmode & 7] . $tmp;
  357.     substr($tmp,2,1) =~ tr/-x/Ss/ if -u _;
  358.     substr($tmp,5,1) =~ tr/-x/Ss/ if -g _;
  359.     substr($tmp,8,1) =~ tr/-x/Tt/ if -k _;
  360.     $perms .= $tmp;
  361.  
  362.     $user = $user{$uid} || $uid;
  363.     $group = $group{$gid} || $gid;
  364.  
  365.     ($sec,$min,$hour,$mday,$mon,$year) = localtime($mtime);
  366.     $moname = $moname[$mon];
  367.     if (-M _ > 365.25 / 2) {
  368.     $timeyear = $year + 1900;
  369.     }
  370.     else {
  371.     $timeyear = sprintf("%02d:%02d", $hour, $min);
  372.     }
  373.  
  374.     printf "%5lu %4ld %-10s %2d %-8s %-8s %8s %s %2d %5s %s\n",
  375.         $ino,
  376.          $blocks,
  377.               $perms,
  378.                 $nlink,
  379.                 $user,
  380.                      $group,
  381.                       $sizemm,
  382.                           $moname,
  383.                          $mday,
  384.                              $timeyear,
  385.                              $pname;
  386.     1;
  387. }
  388.  
  389. sub sizemm {
  390.     sprintf("%3d, %3d", ($rdev >> 8) & 255, $rdev & 255);
  391. }
  392.  
  393. END
  394. }
  395.  
  396. if ($initcpio) {
  397. print <<'START', <<"INTERP", <<'END';
  398. sub cpio {
  399.     local($nc,$fh) = @_;
  400.     local($text);
  401.  
  402.     if ($name eq 'TRAILER!!!') {
  403.     $text = '';
  404.     $size = 0;
  405.     }
  406.     else {
  407. START
  408.     (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$size,
  409.       \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
  410. INTERP
  411.     if (-f _) {
  412.         open(IN, "./$_\0") || do {
  413.         warn "Couldn't open $name: $!\n";
  414.         return;
  415.         };
  416.     }
  417.     else {
  418.         $text = readlink($_);
  419.         $size = 0 unless defined $text;
  420.     }
  421.     }
  422.  
  423.     ($nm = $name) =~ s#^\./##;
  424.     $nc{$fh} = $nc;
  425.     if ($nc eq 'n') {
  426.     $cpout{$fh} .=
  427.       sprintf("%06o%06o%06o%06o%06o%06o%06o%06o%011lo%06o%011lo%s\0",
  428.         070707,
  429.         $dev & 0777777,
  430.         $ino & 0777777,
  431.         $mode & 0777777,
  432.         $uid & 0777777,
  433.         $gid & 0777777,
  434.         $nlink & 0777777,
  435.         $rdev & 0177777,
  436.         $mtime,
  437.         length($nm)+1,
  438.         $size,
  439.         $nm);
  440.     }
  441.     else {
  442.     $cpout{$fh} .= "\0" if length($cpout{$fh}) & 1;
  443.     $cpout{$fh} .= pack("SSSSSSSSLSLa*",
  444.         070707, $dev, $ino, $mode, $uid, $gid, $nlink, $rdev, $mtime,
  445.         length($nm)+1, $size, $nm . (length($nm) & 1 ? "\0" : "\0\0"));
  446.     }
  447.     if ($text ne '') {
  448.     $cpout{$fh} .= $text;
  449.     }
  450.     elsif ($size) {
  451.     &flush($fh) while ($l = length($cpout{$fh})) >= 5120;
  452.     while (sysread(IN, $cpout{$fh}, 5120 - $l, $l)) {
  453.         &flush($fh);
  454.         $l = length($cpout{$fh});
  455.     }
  456.     }
  457.     close IN;
  458. }
  459.  
  460. sub flush {
  461.     local($fh) = @_;
  462.  
  463.     while (length($cpout{$fh}) >= 5120) {
  464.     syswrite($fh,$cpout{$fh},5120);
  465.     ++$blocks{$fh};
  466.     substr($cpout{$fh}, 0, 5120) = '';
  467.     }
  468. }
  469.  
  470. sub flushall {
  471.     $name = 'TRAILER!!!';
  472.     foreach $fh (keys %cpout) {
  473.     &cpio($nc{$fh},$fh);
  474.     $cpout{$fh} .= "0" x (5120 - length($cpout{$fh}));
  475.     &flush($fh);
  476.     print $blocks{$fh} * 10, " blocks\n";
  477.     }
  478. }
  479.  
  480. END
  481. }
  482.  
  483. if ($inittar) {
  484. print <<'START', <<"INTERP", <<'END';
  485. sub tar {
  486.     local($fh) = @_;
  487.     local($linkname,$header,$l,$slop);
  488.     local($linkflag) = "\0";
  489.  
  490. START
  491.     (\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid,\$rdev,\$size,
  492.       \$atime,\$mtime,\$ctime,\$blksize,\$blocks) = $stat\(_\);
  493. INTERP
  494.     $nm = $name;
  495.     if ($nlink > 1) {
  496.     if ($linkname = $linkseen{$fh,$dev,$ino}) {
  497.         $linkflag = 1;
  498.     }
  499.     else {
  500.         $linkseen{$fh,$dev,$ino} = $nm;
  501.     }
  502.     }
  503.     if (-f _) {
  504.     open(IN, "./$_\0") || do {
  505.         warn "Couldn't open $name: $!\n";
  506.         return;
  507.     };
  508.     $size = 0 if $linkflag ne "\0";
  509.     }
  510.     else {
  511.     $linkname = readlink($_);
  512.     $linkflag = 2 if defined $linkname;
  513.     $nm .= '/' if -d _;
  514.     $size = 0;
  515.     }
  516.  
  517.     $header = pack("a100a8a8a8a12a12a8a1a100",
  518.     $nm,
  519.     sprintf("%6o ", $mode & 0777),
  520.     sprintf("%6o ", $uid & 0777777),
  521.     sprintf("%6o ", $gid & 0777777),
  522.     sprintf("%11o ", $size),
  523.     sprintf("%11o ", $mtime),
  524.     "        ",
  525.     $linkflag,
  526.     $linkname);
  527.     $l = length($header) % 512;
  528.     substr($header, 148, 6) = sprintf("%6o", unpack("%16C*", $header));
  529.     substr($header, 154, 1) = "\0";  # blech
  530.     $tarout{$fh} .= $header;
  531.     $tarout{$fh} .= "\0" x (512 - $l) if $l;
  532.     if ($size) {
  533.     &tflush($fh) while ($l = length($tarout{$fh})) >= 10240;
  534.     while (sysread(IN, $tarout{$fh}, 10240 - $l, $l)) {
  535.         $slop = length($tarout{$fh}) % 512;
  536.         $tarout{$fh} .= "\0" x (512 - $slop) if $slop;
  537.         &tflush($fh);
  538.         $l = length($tarout{$fh});
  539.     }
  540.     }
  541.     close IN;
  542. }
  543.  
  544. sub tflush {
  545.     local($fh) = @_;
  546.  
  547.     while (length($tarout{$fh}) >= 10240) {
  548.     syswrite($fh,$tarout{$fh},10240);
  549.     ++$blocks{$fh};
  550.     substr($tarout{$fh}, 0, 10240) = '';
  551.     }
  552. }
  553.  
  554. sub tflushall {
  555.     local($len);
  556.  
  557.     foreach $fh (keys %tarout) {
  558.     $len = 10240 - length($tarout{$fh});
  559.     $len += 10240 if $len < 1024;
  560.     $tarout{$fh} .= "\0" x $len;
  561.     &tflush($fh);
  562.     }
  563. }
  564.  
  565. END
  566. }
  567.  
  568. exit;
  569.  
  570. ############################################################################
  571.  
  572. sub tab {
  573.     local($tabstring);
  574.  
  575.     $tabstring = "\t" x ($indent / 2) . ' ' x ($indent % 2 * 4);
  576.     if (!$statdone) {
  577.     if ($_ =~ /^(name|print|prune|exec|ok|\(|\))/) {
  578.         $delayedstat++;
  579.     }
  580.     else {
  581.         if ($saw_or) {
  582.         $tabstring .= <<"ENDOFSTAT" . $tabstring;
  583. (\$nlink || ((\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid) = $stat\(\$_\))) &&
  584. ENDOFSTAT
  585.         }
  586.         else {
  587.         $tabstring .= <<"ENDOFSTAT" . $tabstring;
  588. ((\$dev,\$ino,\$mode,\$nlink,\$uid,\$gid) = $stat\(\$_\)) &&
  589. ENDOFSTAT
  590.         }
  591.         $statdone = 1;
  592.     }
  593.     }
  594.     $tabstring =~ s/^\s+/ / if $out =~ /!$/;
  595.     $tabstring;
  596. }
  597.  
  598. sub fileglob_to_re {
  599.     local($tmp) = @_;
  600.  
  601.     $tmp =~ s#([./^\$()])#\\$1#g;
  602.     $tmp =~ s/([?*])/.$1/g;
  603.     "^$tmp\$";
  604. }
  605.  
  606. sub n {
  607.     local($n) = @_;
  608.  
  609.     $n =~ s/^-/< / || $n =~ s/^\+/> / || $n =~ s/^/== /;
  610.     $n =~ s/ 0*(\d)/ $1/;
  611.     $n . ')';
  612. }
  613.  
  614. sub quote {
  615.     local($string) = @_;
  616.     $string =~ s/'/\\'/;
  617.     "'$string'";
  618. }
  619. !NO!SUBS!
  620.  
  621. close OUT or die "Can't close $file: $!";
  622. chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
  623. exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';
  624. chdir $origdir;
  625.